<template>
   <a-card class="page-main page-main-admin" :bordered="false">
   
    <div class="container">
      <div class="hte-head-content">
        <span class="bold-font">类似群范围维护</span>
  
      </div>
    <div class="content-admin">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24"  class="hte-col-nowrap">
          <a-col :span="8">
            <a-form-item label="商标类型"  prop="brandType">
              <a-input v-model="queryParam.brandType" placeholder="请输入商标类型"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="商标价值"  prop="brandValue">
              <a-input v-model="queryParam.brandValue" placeholder="请输入商标价值"  ></a-input>
            </a-form-item>
          </a-col>
         <a-button type="primary" icon="search" @click="searchQuery">查询</a-button>
         <a-button type="default" style="margin-left:16px" @click="searchReset">重置</a-button>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button v-has="'brandValueSimilitude:add,brand:admin'"  @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button v-has="'brandValueSimilitude:export,brand:admin'" type="primary" icon="download" @click="handleExportXls('商标价值应覆盖类似群')">导出</a-button>
      <!-- <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload> -->
        <a-button v-has="'brandValueSimilitude:import,brand:admin'" type="primary" icon="import" @click="excelVisible=true">导入</a-button>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu v-has="'brandValueSimilitude:delete,brand:admin'" slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-has="'brandValueSimilitude:edit,brand:admin'" @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-has="'brandValueSimilitude:delete,brand:admin'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>
  </div> </div>
    <brand-value-similitude-modal ref="modalForm" @ok="modalFormOk"></brand-value-similitude-modal>
    <excel-paste :columnsExcel="columnsExcel" :exportXlsTemplateUrl="exportXlsTemplateUrl"  :excelVisible="excelVisible" :excelConfirmLoading="excelConfirmLoading"   @handleOkExcel="handleOkExcel" @handleCancelExcel="handleCancelExcel"></excel-paste>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import BrandValueSimilitudeModal from './modules/BrandValueSimilitudeModal'
  import { httpAction, getAction,postAction } from '@/api/manage'
  import ExcelPaste from '@/components/excelPaste/ExcelPaste'
  //应覆盖类似群正则

  var similitudeGroupCodesReg =  /^[0-9]+((,[0-9]+)*)?$/;
 
  export default {
    name: 'BrandValueSimilitudeList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      BrandValueSimilitudeModal,ExcelPaste
    },
    data () {
      return {
        description: '商标价值应覆盖类似群管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'商标类型',
            align:"center",
            dataIndex: 'brandType'
          },
          {
            title:'商标价值',
            align:"center",
            dataIndex: 'brandValue'
          },
          {
            title:'类似群应当覆盖范围',
            align:"center",
            dataIndex: 'similitudeGroupCodes'
          },
          // {
          //   title:'乐观锁',
          //   align:"center",
          //   dataIndex: 'version'
          // },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createUser'
          },
          {
            title:'创建人账号',
            align:"center",
            dataIndex: 'createUserId'
          },
           {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime',
               customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'修改人',
            align:"center",
            dataIndex: 'lastUser'
          },
          {
            title:'修改账号',
            align:"center",
            dataIndex: 'lastUserId'
          },
          {
            title:'修改时间',
            align:"center",
            dataIndex: 'lastTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          // {
          //   title:'删除标识',
          //   align:"center",
          //   dataIndex: 'isDel'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/knowledge/brandValueSimilitude/list",
          delete: "/knowledge/brandValueSimilitude/delete",
          deleteBatch: "/knowledge/brandValueSimilitude/deleteBatch",
          exportXlsUrl: "/knowledge/brandValueSimilitude/exportXls",
          importExcelUrl: "knowledge/brandValueSimilitude/importExcel",
          addBatch: "/knowledge/brandValueSimilitude/addBatch",
          
          
        },
        dictOptions:{},
        superFieldList:[],
          columnsExcel:[
           {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            },
            isImportExcelColumns:false
          },
          {
            title:'商标类型', 
             align:"center",
            dataIndex: 'brandType'
          },
          {
            title:'商标价值', 
             align:"center",
            dataIndex: 'brandValue'
          },
          {
            title:'类似群应当覆盖范围', 
             align:"center",
            dataIndex: 'similitudeGroupCodes'
          }
          
        ],
        excelVisible:false,
        excelConfirmLoading:false,
        excelDataSource:[],
        exportXlsTemplateUrl:'templates/excel/brandValueSimilitudeTemplate.xlsx'
      }
    },
  
    created() {
    this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
        handleOkExcel:function(data){

        
        this.excelDataSource = [];
        for (let index = 0; index < data.length; index++) {
          const element = data[index];
          if(!element.brandType || !element.brandValue){
            this.$message.warning(`导入失败商标类型、商标价值不能为空`);
            return;
          }
          let checkData = data.filter(item =>{
            return item.brandType == element.brandType && item.brandValue == element.brandValue; 
          })
          if(!!checkData && checkData.length > 1 ){
            this.$message.warning(`导入失败:${element.brandType + '、'+item.brandValue }商标类型、商标价值重复`);
            return;
          }
          if(!!element.similitudeGroupCodes){
              if(!similitudeGroupCodesReg.test(element.similitudeGroupCodes)){
                  this.$message.warning(`导入失败:类似群应当覆盖范围字段${element.similitudeGroupCodes}请输入数字和英文逗号{,}`);
                  return;
              } 
          }
         
        }

        this.excelDataSource = data;
        //数据验证
        this.excelConfirmLoading = true;
        postAction(this.url.addBatch,data).then(res => {
          this.excelConfirmLoading = false;
            if(res.code === 200){
              
              this.excelVisible = false;
              this.$message.success("导入成功");
              this.loadData();
            }else{
              this.$message.warning('操作异常:'+res.message);
            }
        }).catch(res=>{
                this.excelConfirmLoading = false;
                this.$message.warning('操作异常:'+res.message);
                
            })
         
       
      },
     handleCancelExcel:function(){
        this.excelVisible = false;
       
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'brandType',text:'商标类型'})
        fieldList.push({type:'string',value:'brandValue',text:'商标价值'})
        fieldList.push({type:'string',value:'similitudeGroupCodes',text:'类似群应当覆盖范围'})
        fieldList.push({type:'int',value:'version',text:'乐观锁'})
        fieldList.push({type:'string',value:'createUser',text:'创建人'})
        fieldList.push({type:'string',value:'createUserId',text:'创建人账号'})
        fieldList.push({type:'string',value:'lastUser',text:'修改人'})
        fieldList.push({type:'string',value:'lastUserId',text:'修改账号'})
        fieldList.push({type:'date',value:'lastTime',text:'修改时间'})
        fieldList.push({type:'string',value:'isDel',text:'删除标识'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped lang="less">
   @import '~@assets/less/common.less';
  @import '~@assets/less/hte-main.less';
  .table-operator{margin-top: 10px;}
</style>